<?php
require_once("Panel/NavigModel.php");

class NavigController {
	private $_parameters;
	
	function __construct($__parameters=null){
		$this->_parameters = explode(';',$__parameters);
		// sciezka do plikow tpl
		// jesli zamieszczono pliki tpl dla Allegro w theme to je wczytuj
		// jesli nie to domyslne pliki z view/Allegro
	}
	
	function indexAction($__parameters=null){
		global $smarty, $settings,$config;
		$this->_parameters = explode(';',$__parameters);
		$__pages = NavigModel::_get_page_menu($this->_parameters[0]);
		$smarty->assign('pages',$__pages);
		$smarty->assign('parameters',$_REQUEST['parameters']);
		if (!isset($this->_parameters[2]) && empty($this->_parameters[2]) ) {
			if(is_file("../application/theme/theme_{$settings['szablon']}/{$settings['ramka_theme']}_{$settings['ramka_theme_kolor']}/{$this->_parameters[1]}_menu.tpl")){
				$szablon = $settings['szablon'];
				$theme = $settings['ramka_theme'];
				$kolor = $settings['ramka_theme_kolor'];
				$miejsce = $this->_parameters[1];
				$smarty->display("theme_{$szablon}/{$theme}_{$kolor}/{$miejsce}_menu.tpl");
			}else{
				$smarty->display("menus/{$this->_parameters[1]}_menu.tpl");
			}
		}else{			
			$__pages = NavigModel::_get_page_menu($this->_parameters[0],null);
			$this->get_category_list(0,0,$__pages);
			echo $accordion;
		}
	}
	
	private function jQueryMenuAccording($id=null,$auto=false){
		$accordion = "<script type=\"text/javascript\">
				jQuery().ready(function(){
	
					jQuery('#{$id}').accordion({
					".(($auto) ? 
						"active: false,
						header: '.headnavig',
						navigation: true,
						event: 'onclick',
						fillSpace: false," : ""
					)."	
						autoHeight: false 
					});
				});
			</script>
			";
		echo $accordion;
	}

	/**
	 * generuje tree
	 * @param $id_kategoria_parent
	 * @param $id_kategoria_back
	 * @param $category
	 * @param $setup
	 * @return unknown_type
	 */
	function get_category_list($id_kategoria_parent,$id_kategoria_back,$category,$poziom=0){
		global $accordion;
		$i = 0;
		$sub = 'open';
		$czy_jest_nastepny_poziom = false;
		if ($sub){
				foreach ($category as $key1 => $rows) {
					if ( $rows['id_p_page'] == $id_kategoria_parent ){	
						$czy_jest_nastepny_poziom = true;
						if ($sub == 'open'){ 
									
							
							if ($rows['id_p_page'] == 0){
								// opcja zwijania podmenu jQuery
								//$this->jQueryMenuAccording("{$rows['id_page']}navigation",true);
								echo "<ul id=\"{$rows['id_page']}navigation\" class=\"sidemenu\">";
							}else{ echo "<ul>"; }
							// jeden pusty li nad menu
							//if ($rows['id_p_page'] == 0) echo "<li class=\"top\">&nbsp;</li>";
							$sub = 'close';				
						}
						tools::_assign('categ',$rows);						
						echo "<li>"; 
						
						if ($rows['id_p_page'] == 0) $head__ = "class=\"headnavig\"";
						else  $head__ ="";
							
						if ($rows['redirect']){ echo "<a {$head__} href=\"{$rows['to_url']}\" target=\"_blank\">{$rows['page_name']}</a> "; }
						else echo "<a {$head__} ".(($rows['page_url'] == $_REQUEST['page'])? "class=\"sel\"" : ""  )." href=\"{$rows['page_url']}.html\">{$rows['page_name']}</a> ";

						$this->get_category_list($rows['id_page'],$rows['id_p_page'],$category,$poziom+1);
						
						echo '</li>';
						$i++;
					}
				}			
			if ($sub == 'close'){
				// jeden pusty li pod menu
				//if ($poziom == 0) echo "<li class=\"bot\">&nbsp;</li>";
				echo '</ul>';
			}
		}
	}	
	
	
	/**
	 * zwraca nazwe strony na podstawie adresu url
	 *
	 * @param arr $__parameters
	 */
	function get_page_name($__parameters=null){
		echo NavigModel::_get_page_name($this->_parameters[0]);
	}
	
	function get_default_page_atribut($__parameters=null){
		echo theme::_get_default_page_info($this->_parameters[0],$this->_parameters[1]);
	}
	
	/**
	 * zwraca atrybut url dla wybranej wersji jezykowej
	 *
	 * @param array $__parameters
	 * $__parameters[0] - nazwa atrybutu
	 * $__parameters[1] - jezyk
	 * $__parameters[2] - id_page
	 */
	function get_page_url_for_lang($__parameters=null){	
		$this->_parameters = explode(';',$__parameters);
		if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])){
			echo theme::_get_page_atribut($this->_parameters[0],$this->_parameters[1],theme::_get_page_id($_REQUEST['page']));
		}else{
			echo model_pages::get_default_page_atribut(array('url',$this->_parameters[1]));
		}

	}
	
	function show_submenu($__parameters=null){
		global $smarty;
		if ( !is_null($_REQUEST['page']) && !empty($_REQUEST['page'])){
			$__id_page = theme::_get_page_id($_REQUEST['page']);
		}else{
			$__id_page = theme::_get_default_page_info("id_page");
		}
		$__pages = NavigModel::_get_page_menu($this->_parameters[0],$__id_page);
		$smarty->assign('pages',$__pages);
		$smarty->display("menus/{$this->_parameters[1]}_menu.tpl");
	}
}
?>